#!/usr/bin/perl

use strict;
use Carp;

$ENV{LC_ALL} = "C";

my %queue_info = get_queue_info();
print %queue_info;
exit 0;

sub get_queue_info {
    my %queue;
    my $R            = 0;
    my $nR           = 0;
    my $Q            = 0;
    my $firstqueuing = "none";
    my %queue_info;

#####check and run command#####
    my $cmd   = "qstat";
    my $check = `which $cmd 2>/dev/null`;
    chomp $check;
    if ( !$check ) {
        carp "Could not locate $cmd!";
    }
    open IN, "$check -f|" or carp "Could not run $cmd: $!";
#####

    my $job;
    while ( my $line = <IN> ) {
        if ( $line =~ /^Job Id: (\S+)$/ ) {

            # this is a job line
            $job = $1;
        }
        elsif ( $line =~
/^\s+(\S+) = ((\S+)|((\S+)\s+(\S+)\s+(\S+)\s+(\d+):(\d+):(\d+)\s+(\d+)))$/
          )
        {

#$1=title,(if this is a sigle value $3=value), (if this is a date $5=wk,$6=day,$7=mth,$8$9$10=ss:mm:hh,$11=yr)
            my $key = $1;

            if ( $key eq "Job_Name" ) {
                $3 =~ /(\d+):ppn=(\d+)/;
                $queue{$job}{$key} = $3;
            }

            #print "$1\n";
            if ( $key eq "job_state" ) {
                $queue{$job}{$key} = $3;
                ##pass release job
                if ( $queue{$job}{"Job_Name"} ne "release_1_node" ) {
                    ##find running job
                    if ( $queue{$job}{$key} eq "R" ) { $R++; }
                    else {
                        ##find queuing job
                        if ( $queue{$job}{$key} eq "Q" ) {
                            $Q++;
                            ##mark the first queuing job
                            if ( $firstqueuing eq "none" ) {
                                $firstqueuing = $job;
                            }
                        }
                        $nR++;
                    }
                }
            }
            if ( $key eq "qtime" ) {
              SWITCH: {
                    if ( $6 =~ /Jan/ ) {
                        $queue{$job}{$key}{MM} = "01";
                        last SWITCH;
                    }
                    if ( $6 =~ /Feb/ ) {
                        $queue{$job}{$key}{MM} = "02";
                        last SWITCH;
                    }
                    if ( $6 =~ /Mar/ ) {
                        $queue{$job}{$key}{MM} = "03";
                        last SWITCH;
                    }
                    if ( $6 =~ /Apr/ ) {
                        $queue{$job}{$key}{MM} = "04";
                        last SWITCH;
                    }
                    if ( $6 =~ /May/ ) {
                        $queue{$job}{$key}{MM} = "05";
                        last SWITCH;
                    }
                    if ( $6 =~ /Jun/ ) {
                        $queue{$job}{$key}{MM} = "06";
                        last SWITCH;
                    }
                    if ( $6 =~ /Jul/ ) {
                        $queue{$job}{$key}{MM} = "07";
                        last SWITCH;
                    }
                    if ( $6 =~ /Aug/ ) {
                        $queue{$job}{$key}{MM} = "08";
                        last SWITCH;
                    }
                    if ( $6 =~ /Sep/ ) {
                        $queue{$job}{$key}{MM} = "09";
                        last SWITCH;
                    }
                    if ( $6 =~ /Oct/ ) {
                        $queue{$job}{$key}{MM} = "10";
                        last SWITCH;
                    }

                    if ( $6 =~ /Nov/ ) {
                        $queue{$job}{$key}{MM} = "11";
                        last SWITCH;
                    }
                    if ( $6 =~ /Dec/ ) {
                        $queue{$job}{$key}{MM} = "12";
                        last SWITCH;
                    }
                }
                $queue{$job}{$key}{DD} = $7;
                $queue{$job}{$key}{hh} = $8;
                $queue{$job}{$key}{mm} = $9;
                $queue{$job}{$key}{ss} = $10;
                $queue{$job}{$key}{YY} = $11;
            }
            if ( $key eq "Resource_List.nodes" ) {
                $3 =~ /(\d+):ppn=(\d+)/;
                if ( $2 == 1 ) {
                    $queue{$job}{cpu} = $1;
                }
                else {
                    $queue{$job}{cpu} = $1 * 4;
                }
            }
        }
    }
    close IN;

    if ( $R == 0 and $Q > 0 ) {

        %queue_info = (
            status          => 'stuck',
            required_node   => $queue{$firstqueuing}{cpu},
            jobfirstqueuing => $firstqueuing
        );

        #   printf "%.3u%-64s\nqueue stuck\n", $queue{$firstqueuing}{cpu},
        $firstqueuing;
        return %queue_info;
    }
    elsif ( $R > 0 ) {

        %queue_info = (
            status          => 'running',
            required_node   => 0,
            jobfirstqueuing => $firstqueuing
        );

        #   printf "000%-64s\nqueue running\n", $firstqueuing;
        return %queue_info;
    }
    else {

        %queue_info = (
            status          => 'other',
            required_node   => 0,
            jobfirstqueuing => $firstqueuing
        );

        #   printf "000%-64s\nother state\n", $firstqueuing;
        return %queue_info;
    }

    #Debug info
    #print "R=$R nR=$nR\n";
    #for my $j ( sort keys %queue ) {
    #    if ( $queue{$j}{job_state} eq "R" ) {
    #        print "$j:\n";
    #        print "\tstate=$queue{$j}{job_state}\n";
    #        printf "\ttime=%.4u ", $queue{$j}{qtime}{YY};
    #        printf "%.2u ",        $queue{$j}{qtime}{MM};
    #        printf "%.2u ",        $queue{$j}{qtime}{DD};
    #        printf "%.2u ",        $queue{$j}{qtime}{hh};
    #        printf "%.2u ",        $queue{$j}{qtime}{mm};
    #        printf "%.2u \n",      $queue{$j}{qtime}{ss};
    #    }
    #}
}

